<?php
	include("includes/connect_db.php");

	$aksi = $_GET['aksi'];
	if ($aksi == 1) {
		$id_inbox = $_POST['id_inbox'];
		$sql_update = "UPDATE inbox SET Status=2 WHERE ID='$id_inbox'";
		echo $sql_update;
		$query_update = mysql_query($sql_update);
	}

	$sql_data = "SELECT * FROM inbox WHERE Notif = 0 and is_shown = false LIMIT 1";
	$query_data = mysql_query($sql_data);
	$jumlah_data = mysql_num_rows($query_data);
	$query_data = mysql_query($sql_data);
	$data = mysql_fetch_array($query_data);
	$pesan = $data['TextDecoded'];
	$id = $data['ID'];

	if ($data['counter'] == 4) {
		$sql_update = "UPDATE inbox SET Notif = 1, counter = 5 WHERE ID='$id'";
	} else {
		$counter_inbox = $data['counter'] + 1;
		$sql_update = "UPDATE inbox SET counter = $counter_inbox, is_shown = true WHERE ID='$id'";
	}
	$query_update = mysql_query($sql_update);

	$sql_kontak = "SELECT * FROM tb_kontak WHERE no_hp='$data[SenderNumber]'";
	if ($query_kontak = mysql_query($sql_kontak)) {
		$jumlah = mysql_num_rows($query_kontak);
	} {
		$jumlah = 0;
	}
	


	if ($jumlah == 0) {
		$nomor = $data['SenderNumber'];
	} else {
		$query_kontak = mysql_query($sql_kontak);
		$kontak = mysql_fetch_array($query_kontak);
		$nomor = $kontak['nama_kontak'];
	}

	$arrNotif = array();
?>

<?php
	if ($jumlah_data > 0) {
		$dataArr[nomor] = substr($nomor, 0, 20);
		if (strlen($nomor) > 20) {
		    $dataArr[nomor] .= "...";
		} 

		$dataArr[pesan] = substr($pesan, 0, 80);
		if (strlen($pesan) > 80) {
	        $dataArr[pesan] .= "...";
	    }

	    $dataArr[idPesan] = $id;
	    array_push($arrNotif, $dataArr);
	    echo json_encode($arrNotif);
	}
?>

